package com.info.zhiduoduo.pay.service;

import com.info.zhiduoduo.common.configs.TransferPayConfig;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.pay.formbean.transferpay.TransferAgentPayFormBean;
import com.info.zhiduoduo.pay.formbean.transferpay.TransferQueryAgentPayFormBean;
import com.info.zhiduoduo.pay.sdk.transferpay.model.request.AgentPayReqDTO;
import com.info.zhiduoduo.pay.sdk.transferpay.model.request.QueryAgentPayReqDTO;
import com.info.zhiduoduo.pay.sdk.transferpay.model.request.QueryBalanceReqDTO;
import com.info.zhiduoduo.pay.sdk.transferpay.model.response.AgentPayResDTO;
import com.info.zhiduoduo.pay.sdk.transferpay.model.response.BaseResDTO;
import com.info.zhiduoduo.pay.sdk.transferpay.model.response.QueryAgentPayResDTO;
import com.info.zhiduoduo.pay.sdk.transferpay.model.response.QueryBalanceResDTO;
import com.info.zhiduoduo.pay.sdk.transferpay.service.TransferPayApiService;
import com.info.zhiduoduo.repository.entity.pay.PaymentMerchant;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class TransferPayService {

	@Autowired
	TransferPayApiService transferPayApiService;
	@Autowired
	TransferPayConfig transferPayConfig;

	public SmartResult<BaseResDTO<QueryBalanceResDTO>> queryBalance(int formBean) {
		PaymentMerchant paymentMerchant = new PaymentMerchant().selectById(formBean);
		if (paymentMerchant == null) return SmartResult.fail(StatusMsg.StatusMsg_463);

		QueryBalanceReqDTO reqDTO = new QueryBalanceReqDTO();
		reqDTO.setAccountnumber(paymentMerchant.getAccountNo()).setAppid(paymentMerchant.getAppid());
		BaseResDTO<QueryBalanceResDTO> resDTO = transferPayApiService.queryBalance(reqDTO, paymentMerchant);
		if (resDTO == null) return SmartResult.fail(StatusMsg.StatusMsg_462);
		return SmartResult.success(resDTO);
	}

	public SmartResult<BaseResDTO<AgentPayResDTO>> agentPay(TransferAgentPayFormBean formBean) {
		PaymentMerchant paymentMerchant = new PaymentMerchant().selectById(formBean.getPaymentMerchant());
		if (paymentMerchant == null) return SmartResult.fail(StatusMsg.StatusMsg_463);

		AgentPayReqDTO reqDTO = AgentPayReqDTO.of(formBean);
		reqDTO.setFromaccountnumber(paymentMerchant.getAccountNo()).setAppid(paymentMerchant.getAppid());
		BaseResDTO<AgentPayResDTO> resDTO = transferPayApiService.agentPay(reqDTO, paymentMerchant);
		if (resDTO == null) return SmartResult.fail(StatusMsg.StatusMsg_462);
		return SmartResult.success(resDTO);
	}

	public SmartResult<BaseResDTO<QueryAgentPayResDTO>> queryAgentPay(TransferQueryAgentPayFormBean formBean) {
		PaymentMerchant paymentMerchant = new PaymentMerchant().selectById(formBean.getPaymentMerchant());
		if (paymentMerchant == null) return SmartResult.fail(StatusMsg.StatusMsg_463);

		QueryAgentPayReqDTO reqDTO = new QueryAgentPayReqDTO(formBean.getTradeNo());
		reqDTO.setAppid(paymentMerchant.getAppid());
		BaseResDTO<QueryAgentPayResDTO> resDTO = transferPayApiService.queryAgentPay(reqDTO, paymentMerchant);
		if (resDTO == null) return SmartResult.fail(StatusMsg.StatusMsg_462);
		return SmartResult.success(resDTO);
	}
}
